package com.black.app.service.impl.my;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.my.AppMyData;
import com.black.app.domain.req.my.mydata.*;
import com.black.app.domain.resp.my.mydata.AppMyDataResp;
import com.black.app.service.my.AppMyDataService;
import com.black.app.mapper.my.AppMyDataMapper;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.black.common.utils.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author 18494
 * @description 针对表【app_my_data(我的数据(8类)表)】的数据库操作Service实现
 * @createDate 2024-10-23 20:49:29
 */
@Service
public class AppMyDataServiceImpl extends ServiceImpl<AppMyDataMapper, AppMyData>
        implements AppMyDataService {


    @Override
    public PageInfo<AppMyDataResp> paginationQuery(AppMyDataQueryReq req) {

//        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());

        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    @Override
    public boolean editWeight(AppMyDataWeightDetailAddReq req) {

        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());

        // 根据用户id查询
        AppMyData db = this.getById(req.getUserId());

        // 数据库找不到数据
        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }
        // 修改数据库中的值
        boolean result = this.baseMapper.updateWeight(req);

        return result;
    }

    @Override
    public boolean editHabit(AppMyDataHabitDetailAddReq req) {

        // 获取当前登录用户的用户信息
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());

        // 根据用户id查询
        AppMyData db = this.getById(req.getUserId());


        // 数据库找不到数据
        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }

        //修改数据库中的值
        boolean result = this.baseMapper.updateHabit(req);

        return result;
    }

    @Override
    public boolean editDrink(AppMyDataDrinkDetailAddReq req) {
        // 获取当前登录用户的用户信息
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());
        req.setIntake(req.getCunt() * 1000);
        // 根据用户id和type == req.getType()查询符合的数据
        AppMyData db = this.getOne(new LambdaQueryWrapper<AppMyData>()
                .eq(AppMyData::getUserId, req.getUserId()).eq(AppMyData::getType, req.getType()));

        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }
        db.setData(db.getData() + 1);

        // 更新数据库中的值
        boolean result = this.updateById(db);

        return result;
    }

    @Override
    public boolean editCalorie(AppMyHeatDetailAddReq req) {
        // 获取当前登录用户的用户信息
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());
        // 根据用户id和type == req.getType()查询符合的数据
        AppMyData db = this.getOne(new LambdaQueryWrapper<AppMyData>()
                .eq(AppMyData::getUserId, req.getUserId()).eq(AppMyData::getType, req.getType()));

        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }
        db.setData(db.getData() + 1);

        // 更新数据库中的值
        boolean result = this.updateById(db);

        return result;
    }

    @Override
    public boolean editMenstrual(AppMyMenstrualDetailAddReq req) {
        // 获取当前登录用户的用户信息
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());
        // 根据用户id和type == req.getType()查询符合的数据
        AppMyData db = this.getOne(new LambdaQueryWrapper<AppMyData>()
                .eq(AppMyData::getUserId, req.getUserId()).eq(AppMyData::getType, req.getType()));

        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }
        db.setData(db.getData() + 1);

        // 更新数据库中的值
        boolean result = this.updateById(db);


        return result;
    }

    @Override
    public boolean editMove(AppMyMoveDetailAddReq req) {
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());
        // 根据用户id和type == req.getType()查询符合的数据
        AppMyData db = this.getOne(new LambdaQueryWrapper<AppMyData>()
                .eq(AppMyData::getUserId, req.getUserId()).eq(AppMyData::getType, req.getType()));

        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }
        db.setData(db.getData() + req.getMove());
        boolean result = this.updateById(db);

        return result;
    }

    @Override
    public boolean editWaist(AppMyWaistlineDetailAddReq req) {
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());
        // 根据用户id和type == req.getType()查询符合的数据
        AppMyData db = this.getOne(new LambdaQueryWrapper<AppMyData>()
                .eq(AppMyData::getUserId, req.getUserId()).eq(AppMyData::getType, req.getType()));

        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }

        db.setData(db.getData() + 1);

        boolean result = this.updateById(db);

        return result;
    }

    @Override
    public boolean editStep(AppMyWalkDetailAddReq req) {
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());
        // 根据用户id和type == req.getType()查询符合的数据
        AppMyData db = this.getOne(new LambdaQueryWrapper<AppMyData>()
                .eq(AppMyData::getUserId, req.getUserId()).eq(AppMyData::getType, req.getType()));

        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }

        db.setData(db.getData() + req.getWalk());

        boolean result = this.updateById(db);


        return result;
    }


}




